<template>
      <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="auto"
          :size="formSize"
      >
	  <div class="register-container">
	  <header class="title-section">
	      <img src="@/assets/jhk-1719213427217.jpg" alt="背景图片" class="image"/>
	      <h1>东软环保公众监督平台</h1>
	  </header>  
        <el-form-item label="手机号" prop="telId">
          <el-input v-model="ruleForm.telId" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="ruleForm.realName" placeholder="真实姓名便于我们联系你"/>
        </el-form-item>
        <!--		<el-form-item label="年龄" prop="age">-->
        <!--		      <el-input v-model="ruleForm.age" placeholder="请填写你的年龄" />-->
        <!--		    </el-form-item>-->

        <el-form-item label="生日" required prop="birthday">
          <el-date-picker
              v-model="ruleForm.birthday"
              type="date"
              aria-label="请选择生日"
              placeholder="请选择生日"
              style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="ruleForm.sex">
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="重复密码" prop="repassword">
          <el-input type="password" v-model="ruleForm.repassword" placeholder="请再次输入密码"/>
        </el-form-item>
		<div class="button-container">
          <el-button   type="primary" @click="submitForm" class="register-button">
            注册
          </el-button>
          <el-button type="primary" @click="resetForm" class="register-button">重置</el-button>
		</div>
		</div>
      </el-form>
    
</template>

<script setup>

import {reactive, ref} from 'vue'
import axios from 'axios'
import qs from 'qs'
import {useRouter} from 'vue-router';
import {ElMessage} from "element-plus";

const formSize = ref('default')
const ruleFormRef = ref()
const ruleForm = reactive({
  telId: '',
  realName: '',
  birthday: '',
  sex: '1',
  password: '',
  repassword: '',
})

const rules = reactive({
  telId: [
    {required: true, message: '请输入手机号', trigger: 'blur'},
    {min: 11, max: 11, message: '手机号长度应为11位', trigger: 'blur'}
  ],
  realName: [
    {required: true, message: '请输入真实姓名', trigger: 'blur'},
    {min: 2, max: 20, message: '真实姓名长度应在2-20之间', trigger: 'blur'}
  ],
  sex: [
    {
      required: true,
      message: '请输入性别',
      trigger: 'change'
    }
  ],
  birthday: [
    {
      type: 'date',
      required: true,
      message: '请选择生日',
      trigger: 'change'
    }
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度应在6-20之间', trigger: 'blur'}
  ],
  repassword: [
    {required: true, message: '请输入重复密码', trigger: 'blur'},
    {min: 6, max: 20, message: '重复密码长度应在6-20之间', trigger: 'blur'},
    {
      validator: (rule, value, callback) => {
        if (value !== ruleForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
})
let formRef = ref(null)
let router = useRouter()
const submitForm = async () => {
  /*router.push({
    path:'/'
    });*/
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      //let data = qs.stringify(ruleForm)
      let data = [{
        password: ruleForm.password,
        phoneNum: ruleForm.telId,
        birthday: ruleForm.birthday,
        gender: ruleForm.sex,
        name: ruleForm.realName
      }]
      console.log(data)
      axios.post("http://localhost:8085/supervisor/addSupervisor", data)
          .then(res => {
            console.log(res)
            if (res.data.code == 1) {
              ElMessage.success("注册成功")
              router.push({
                path: '/supervisor/login'
              });
            } else{
              ElMessage.error(res.data.message)
            }
          })
    } else {
      ElMessage.error("请完整填写条目！")
      //console.log('表单校验未通过', fields)
    }
  })
}

const resetForm = () => {
  ruleFormRef.value.resetFields()
}
</script>

<style scoped>

.register-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #e0f7fa; /* 蓝绿色背景色 */
   min-height: 100vh;
}
.title-section {
  text-align: center;
}
h1 {
  color: green;
}
.image {
    display: block;
    margin: auto;
	width: 100%;
    height: auto;
  }
  .register-button {
    background-color: #70b0bc; /* 蓝绿色按钮 */
    border-color: #00796b;
    width: 120px;
    height: 50px;
    border-radius: 8px;
	/* margin-right: 20px; */
  }
  .button-container {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;
    }
</style>
